#include "ResetPasswordDialog.h"

ResetPasswordDialog::ResetPasswordDialog(const QString &username, QWidget *parent)
    : QDialog(parent), username(username)
    , strengthLabel(new QLabel(this))
    , ruleLabel(new QLabel("  密码规则：至少6个字符，包含大写字母、小写字母、数字和特殊字符。",this))
{

    setWindowTitle("重置密码");
    setFixedWidth(400);
    QLabel *labelNewPassword = new QLabel("新密码：", this);
    QLabel *labelConfirmPassword = new QLabel("确认密码：", this);
    labelNewPassword->setFixedWidth(70);
    labelConfirmPassword->setFixedWidth(70);
    ruleLabel->setWordWrap(true);
    ruleLabel->setStyleSheet("color:orange; font-size:14px;");
    ruleLabel->setContentsMargins(0, 10, 0, 0);


    lineEditNewPassword = new QLineEdit(this);
    lineEditNewPassword->setFixedWidth(200);
    lineEditConfirmPassword = new QLineEdit(this);
    lineEditConfirmPassword->setFixedWidth(200);
    lineEditNewPassword->setEchoMode(QLineEdit::Password);
    lineEditConfirmPassword->setEchoMode(QLineEdit::Password);
    buttonSubmit = new QPushButton("提交", this);
    buttonSubmit->setFixedWidth(210);
    QHBoxLayout *buttonLayout=new QHBoxLayout;
    buttonLayout->addWidget(buttonSubmit);
    buttonLayout->setAlignment(Qt::AlignCenter);
    buttonSubmit->setStyleSheet(R"(
        QPushButton {
            background-color: #17A2B8; /* 青色背景 */
            color: white; /* 白色文字 */
            border-radius: 10px; /* 圆角 */
            padding: 8px 16px; /* 内边距 */
            font-size: 14px; /* 字体大小 */
        }
        QPushButton:hover {
            background-color: #138496; /* 悬停时的深青色 */
        }
        QPushButton:pressed {
            background-color: #117a8b; /* 按下时的更深青色 */
        }
    )");

    QVBoxLayout *layout = new QVBoxLayout(this);
    QHBoxLayout *newPasswordLayout=new QHBoxLayout();
    QHBoxLayout *confirmPasswordLayout=new QHBoxLayout();
    newPasswordLayout->addWidget(labelNewPassword);
    newPasswordLayout->addWidget(lineEditNewPassword);
    newPasswordLayout->addWidget(strengthLabel);
    confirmPasswordLayout->addWidget(labelConfirmPassword);
    confirmPasswordLayout->addWidget(lineEditConfirmPassword);
    confirmPasswordLayout->setAlignment(Qt::AlignLeft);
    layout->addLayout(newPasswordLayout);
    layout->addLayout(confirmPasswordLayout);
    layout->addLayout(buttonLayout);
    layout->addWidget(ruleLabel);


    connect(buttonSubmit, &QPushButton::clicked, this, &ResetPasswordDialog::resetPassword);

    strengthLabel->setText("输入新密码");
    strengthLabel->setStyleSheet("color:blue");
    connect(lineEditNewPassword,&QLineEdit::textChanged,this,[=](const QString &newPassword){
        bool hasUppercase=newPassword.contains(QRegExp("[A-Z]"));
        bool hasLowercase=newPassword.contains(QRegExp("[a-z]"));
        bool hasDigit=newPassword.contains(QRegExp("\\d"));

        int strengthLevel= hasUppercase+hasLowercase+ hasDigit;

        if(strengthLevel==1){
            strengthLabel->setText("密码强度：弱");
            strengthLabel->setStyleSheet("color:red");
        }else if(strengthLevel==2){
            strengthLabel->setText("密码强度：中");
            strengthLabel->setStyleSheet("color:orange");

        }else if(strengthLevel==3){
            strengthLabel->setText("密码强度：强");
            strengthLabel->setStyleSheet("color:green");
        }
        else{
            strengthLabel->setText("密码强度：弱");
            strengthLabel->setStyleSheet("color:red");
        }
        if(strengthLevel<3||lineEditNewPassword->text().isEmpty()){
            buttonSubmit->setEnabled(false);
            buttonSubmit->setStyleSheet(R"(
                background-color: #17A2B8; /* 青色背景 */
                color: white; /* 白色文字 */
                border-radius: 10px; /* 圆角 */
                padding: 8px 16px; /* 内边距 */
                font-size: 14px; /* 字体大小 */
            )");

        }else{
            buttonSubmit->setEnabled(true);
            buttonSubmit->setStyleSheet(R"(
            QPushButton {
                background-color: #17A2B8; /* 青色背景 */
                color: white; /* 白色文字 */
                border-radius: 10px; /* 圆角 */
                padding: 8px 16px; /* 内边距 */
                font-size: 14px; /* 字体大小 */
            }
            QPushButton:hover {
                background-color: #138496; /* 悬停时的深青色 */
            }
            QPushButton:pressed {
                background-color: #117a8b; /* 按下时的更深青色 */
            }
        )");
        }
    });
}

void ResetPasswordDialog::resetPassword() {
    QString newPassword = lineEditNewPassword->text();
    QString confirmPassword = lineEditConfirmPassword->text();

    if (newPassword.isEmpty() || confirmPassword.isEmpty()) {
        QMessageBox::warning(this, "错误", "密码不能为空！");
        return;
    }

    if (newPassword != confirmPassword) {
        QMessageBox::critical(this, "错误", "两次密码输入不一致！");
        return;
    }

    QSettings settings("MyApp", "LoginDemo");
    settings.beginGroup("user");
    settings.beginGroup(username);
    settings.setValue("password", newPassword);
    settings.endGroup();
    settings.endGroup();

    QMessageBox::information(this, "成功", "密码重置成功！请返回登录");
    close();
}
